package com.zbkj.admin.controller;

import com.google.common.net.MediaType;
import com.zbkj.common.model.log.MetalPrice;
import com.zbkj.common.model.log.SensitiveMethodLog;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.response.CommonResult;
import com.zbkj.common.utils.RestTemplateUtil;
import com.zbkj.service.service.SensitiveMethodLogService;
import io.micrometer.core.ipc.http.HttpSender;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import kong.unirest.HttpResponse;
import kong.unirest.Unirest;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 日志控制器
 *  +----------------------------------------------------------------------
 *  | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 *  +----------------------------------------------------------------------
 *  | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
 *  +----------------------------------------------------------------------
 *  | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 *  +----------------------------------------------------------------------
 *  | Author: CRMEB Team <admin@crmeb.com>
 *  +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("api/admin/log")
@Api(tags = "日志管理")
public class LogController {

    @Autowired
    private SensitiveMethodLogService sensitiveMethodLogService;

    /**
     * 敏感操作日志列表
     */
    @PreAuthorize("hasAuthority('admin:log:sensitive:list')")
    @ApiOperation(value = "敏感操作日志列表")
    @RequestMapping(value = "/sensitive/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<SensitiveMethodLog>> getList(@Validated PageParamRequest pageParamRequest) {
        return CommonResult.success(CommonPage.restPage(sensitiveMethodLogService.getPageList(pageParamRequest)));
    }
    /**
     * 敏感操作日志列表
     */
    @PreAuthorize("hasAuthority('admin:log:sensitive:list')")
    @ApiOperation(value = "获取金价价格")
    @RequestMapping(value = "/sensitive/lists/{id}", method = RequestMethod.GET)
    public CommonResult<String> getLists(@PathVariable String id) {
        String  res="";
        try {

              res = Unirest.post("http://fendesystockapi.tsunyik.com/stock/getOneMetalPrice")
                    .multiPartContent()
                    .field("metal", id)
                    .asString()
                    .getBody();
            return CommonResult.success(res,"success");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.success(res);
    }
    /**
     * 敏感操作日志列表
     */
    @PreAuthorize("hasAuthority('admin:log:sensitive:list')")
    @ApiOperation(value = "获取金价价格")
    @RequestMapping(value = "/sensitive/getMetalPrice", method = RequestMethod.GET)
    public CommonResult<List<MetalPrice>> getMetalPrice() {
        return CommonResult.success(sensitiveMethodLogService.getMetalPrice());
    }
}
